package kt.com.bilin.coin.helper

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.coroutines.delay
import kt.com.bilin.coin.common.permissionRequest
//import com.baidu.ocr.sdk.OCR
//import com.baidu.ocr.sdk.OnResultListener
//import com.baidu.ocr.sdk.exception.OCRError
//import com.baidu.ocr.sdk.model.AccessToken
//import com.baidu.ocr.sdk.model.IDCardParams
//import com.baidu.ocr.sdk.model.IDCardResult
//import com.baidu.ocr.ui.camera.CameraActivity
import kt.com.bilin.coin.common.toastMessageLong
import kt.com.bilin.coin.ui.activity.BaseActivity
import kt.com.bilin.coin.ui.activity.OfflineFaceLivenessActivity
import kt.com.bilin.coin.ui.activity.launchUI
import kt.com.bilin.coin.utils.BLUtils
import kt.com.bilin.coin.utils.Logger
import java.io.File

//private const val REQUEST_CODE_CAMERA = 1
private const val REQUEST_CODE_FACE = 2
//typealias OCRSuccess = ((idnumberWord: String, nameWord: String) -> Unit)?

//fun BaseActivity.initOCR(success: (() -> Unit)? = null) {
//    val accessToken = OCR.getInstance(this).accessToken
//    if (accessToken == null) {
//        OCR.getInstance(applicationContext).initAccessToken(object : OnResultListener<AccessToken> {
//            override fun onResult(accessToken: AccessToken?) {
//                Logger.e("accessToken===>${accessToken}")
//                success?.invoke()
//            }
//
//            override fun onError(error: OCRError?) {
//                Logger.e("error===>${error}")
//                //"实名认证服务初始化失败，请联系客服".toastMessageLong()
//            }
//
//        }, applicationContext)
//    } else {
//        success?.invoke()
//    }
//}
//
//fun BaseActivity.openOCRCardFront() {
//    initOCR {
//        val intent = Intent(this, CameraActivity::class.java)
//        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, getSaveFile(application).absolutePath)
//        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT)
//        intent.putExtra(CameraActivity.KEY_NATIVE_TOKEN, OCR.getInstance(this).license)
//        intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true)
//        startActivityForResult(intent, REQUEST_CODE_CAMERA)
//    }
//}
//
//fun BaseActivity.OCRCardFrontResult(requestCode: Int, resultCode: Int, data: Intent?, success: OCRSuccess) {
//    if (requestCode == REQUEST_CODE_CAMERA && resultCode == AppCompatActivity.RESULT_OK && data != null) {
//        val contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE)
//        val filePath: String = getSaveFile(applicationContext).absolutePath
//        if (!contentType.isNullOrEmpty() && CameraActivity.CONTENT_TYPE_ID_CARD_FRONT == contentType) {
//            recIDCard(filePath, success)
//        }
//    }
//}

//private fun BaseActivity.recIDCard(filePath: String, success: OCRSuccess) {
//    OCR.getInstance(this).recognizeIDCard(IDCardParams().apply {
//        idCardSide = IDCardParams.ID_CARD_SIDE_FRONT
//        imageFile = File(filePath)
//        isDetectDirection = true
//    }, object : OnResultListener<IDCardResult?> {
//        override fun onResult(result: IDCardResult?) {
//            if (result != null) {
//                Logger.e("result==>${result}")
//                val idnumberWord = result.idNumber
//                val nameWord = result.name
//                if (idnumberWord != null) {
//                    Logger.e("idnumberWord==>${idnumberWord.words}")
//                }
//                if (nameWord != null) {
//                    Logger.e("nameWord==>${nameWord.words}")
//                }
//                if (idnumberWord.words.isNullOrEmpty() || nameWord.words.isNullOrEmpty()) {
//                    "身份证识别失败".toastMessageLong()
//                    return
//                }
//                success?.invoke(idnumberWord.words, nameWord.words)
//            }
//        }
//
//        override fun onError(error: OCRError) {
//            "身份证识别错误：${error.message}".toastMessageLong()
//        }
//    })
//}

fun BaseActivity.openFace(start: (() -> Unit)? = null) {
    permissionRequest {
        permission(Permission.CAMERA)
        onGranted {
            goActicity(OfflineFaceLivenessActivity::class.java, requestCode = REQUEST_CODE_FACE)
            launchUI({
                delay(2000)
                start?.invoke()
            })
        }
        onDenied { "授权失败无法使用活体检测".toastMessageLong();start?.invoke() }
    }
}

fun faceResult(requestCode: Int, resultCode: Int, data: Intent?, success: (bestImagePath: String) -> Unit) {
    if (requestCode == REQUEST_CODE_FACE) {
        if (Activity.RESULT_OK == resultCode && data != null) {
            val filePath = data.getStringExtra("bestimage_path")
            if (!filePath.isNullOrEmpty()) {
                success.invoke(filePath)
            } else {
                "活体图片没找到".toastMessageLong()
            }
        } else {
            "活体检测失败".toastMessageLong()
        }
    }
}

fun getSaveFile(context: Context): File {
    return File(context.filesDir, "pic.jpg")
}